layui.use(['form'], function(){
    var form = layui.form;
    form.on('select(columnSelect)', function(data){
        if(data.value==null||data.value==''){
            $('#typeSelect option').remove();
            form.render('select','typeSelect');
            return;
        }
        $.post(ctxPath+'/blog/admin/typecolum/api/find',{upid:data.value},function (res) {
            if(res.obj!=null&&res.obj.length!=0){
                var data=res.obj;
                $('#typeSelect option').remove();
                var appendHtml='<option value="">请先选择栏目</option>';
                for (var i=0;i<data.length;i++){
                    appendHtml+='<option value="'+data[i].id+'">'+data[i].typename+'</option>';
                }
                console.log(appendHtml);
                $('#typeSelect').append(appendHtml);
                form.render('select','typeSelect');
            }
        },'json');
    });
    form.on('select(typeSelect)', function(data){
        $("#listTable").bootstrapTable('refresh');
    });
});
function openInsertWindow(){
    layer.open({
        type: 2,
        title: '添加',
        shadeClose: false,
        shade: 0.5,
        area: ['100%', '100%'],
        content: ctxPath+'/blog/admin/article/addPage', //iframe的url
        end:function(index, layero){
            $("#listTable").bootstrapTable('refresh');
        }
    });
}
function flushArticleCache() {
    $.post(ctxPath+'/blog/admin/article/api/flushArticleData',null,function (res) {
        if(res.res){
            layer.msg('刷新成功',{icon:1});
        }
    },'json');
}
function urlsToBaidu() {
    $.post(ctxPath+'/blog/admin/article/api/urlsToBaidu',null,function (res) {
        console.log(res);
    },'json');
}
function openPositionWindow(id) {
    layer.open({
        type: 2,
        title: '设置文章位置',
        shadeClose: false,
        shade: 0.5,
        area: ['400px', '500px'],
        content: ctxPath+'/blog/admin/article/positionSet?id='+id, //iframe的url
        end:function(index, layero){
            $("#listTable").bootstrapTable('refresh');
        }
    });
}
function publish(id) {
    layer.confirm('确认发布？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        var load = layer.load(1, {
            shade: [0.6,'#000'] //0.1透明度的白色背景
        });
        $.post(ctxPath+'/blog/admin/article/api/update',{id:id,articlestate:'正常'},function(res){
            layer.close(load);
            if(res.resNum==1){
                layer.msg('操作成功',{icon:1});
                setTimeout(function () {
                    $('#listTable').bootstrapTable('refresh');
                },1000);
            }else {
                layer.msg('操作失败',{icon:2});
            }
        },'json');
    }, function(){

    });
}
function del(id){
    layer.confirm('确认删除？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        var load = layer.load(1, {
            shade: [0.6,'#000'] //0.1透明度的白色背景
        });
        $.post(ctxPath+'/blog/admin/article/api/update',{id:id,articlestate:'已删除'},function(res){
            layer.close(load);
            if(res.resNum==1){
                layer.msg('操作成功',{icon:1});
                setTimeout(function () {
                    $('#listTable').bootstrapTable('refresh');
                },1000);
            }else {
                layer.msg('操作失败',{icon:2});
            }
        },'json');
    }, function(){

    });
}
function recover(id){
    layer.confirm('确认恢复？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        var load = layer.load(1, {
            shade: [0.6,'#000'] //0.1透明度的白色背景
        });
        $.post(ctxPath+'/blog/admin/article/api/update',{id:id,articlestate:'正常'},function(res){
            layer.close(load);
            if(res.resNum==1){
                layer.msg('操作成功',{icon:1});
                setTimeout(function () {
                    $('#listTable').bootstrapTable('refresh');
                },1000);
            }else {
                layer.msg('操作失败',{icon:2});
            }
        },'json');
    }, function(){

    });
}
function toEditPage(id){
    layer.open({
        type: 2,
        title: '修改',
        shadeClose: false,
        shade: 0.5,
        area: ['100%', '100%'],
        content: ctxPath+'/blog/admin/article/editPage?id='+id, //iframe的url
        end:function(index, layero){
            $("#listTable").bootstrapTable('refresh');
        }
    });
}
function toLook(id){
    window.open(ctxPath+'/blog/admin/content/'+id+'.html','target','');
}
$(function () {
    var table=$("#listTable").bootstrapTable({
        toolbar:"#listToolbar",
        search:true,
        showRefresh:true,
        showColumns:true,//是否显示 内容列下拉框
        undefinedText:'无',
        striped:true,
        cache:false,
        pageNumber: 1,
        queryParamsType:'limit',//查询参数组织方式
        contentType: "application/x-www-form-urlencoded",//不加这个的话提交参数不会对应到spring mvc两个实体类
        pagination:true,//是否分页
        pageSize:5,//单页记录数
        pageList:[5,10,20,30],//分页步进值
        method: 'post',
        sidePagination:'server',//指定服务器端分页,如果不指定的话，queryParams:function (params) {}方法无法得到params参数
        url: ctxPath+'/blog/admin/article/api/findByPage',
        paginationPreText:'上一页',
        paginationNextText:'下一页',
        queryParams:function (params) {
            // console.log(params.search);//搜索框输入的文本，即搜索内容
            return {
                pageNumber:(params.offset/params.limit)+1,
                pageSize:params.limit,
                // name:params.search
                keyWord:params.search,
                columnid:$('#columnSelect').val(),
                typeid:$('#typeSelect').val()
            };
        },
        responseHandler:function (res) {
            if(res.obj!=null){
                var resObj={//bootstrap table必须要这两个参数，而我使用的是beetl sql PageQuery直接返回的，参数名不一致，所以做此操作
                    total:res.obj.totalRow,
                    rows:res.obj.list
                };
                return resObj;
            }
            var resObj={
                total:0,
                rows:null
            };
            return resObj;
        },
        columns: [
            {
            field: 'id',
            title: '编号',
            width:50
            },
            {
                field: 'title',
                title: '标题',
                width:250,
                formatter:function (value,row,index) {
                    return $.mine.textElip(row.title,40);
                }
            },
            {
                field: 'tiny',
                title: '封面',
                formatter:function (value,row,index) {
                    return '<img src="'+ctxPath+row.tiny+'" style="height: 70px;">';
                }
            },
            {
                field: 'keywords',
                title: 'SEO关键字',
                width:50
            },
            {
                field: 'tips',
                title: '适读提示',
                width:50,
                formatter:function (value,row,index) {
                    return $.mine.textElip(row.tips,20);
                }
            },
            {
                field: 'description',
                title: 'SEO描述',
                width:100,
                formatter:function (value,row,index) {
                    return $.mine.textElip(row.description,20);
                }
            },
            {
                field: 'publishdatetime',
                title: '发布时间',
                width:150,
                formatter:function (value,row,index) {
                    return $.mine.formatDatetime(row.publishdatetime,null);
                }
            },
            {
                field: 'position',
                title: '位置',
                width:100
            },
            {
                field: 'articlestate',
                title: '状态',
                formatter:function (value,row,index) {
                    if(row.articlestate=='正常'){
                        return '<span class="layui-badge layui-bg-blue">正常</span>';
                    }
                    if(row.articlestate=='已删除'){
                        return '<span class="layui-badge">已删除</span>';
                    }
                    if(row.articlestate=='草稿'){
                        return '<span class="layui-badge layui-bg-orange">草稿</span>';
                    }
                }
            },
            {
                field: '',
                title: '操作',
                formatter:function (value,row,index) {
                    var resHtml='';
                    if(row.articlestate=='正常'){
                        resHtml+='<button class="layui-btn layui-btn-sm layui-btn-danger" style="margin-top: 10px;" onclick="del('+row.id+')"><i class="layui-icon">&#xe640;</i>删除</button>';
                    }
                    if(row.articlestate=='已删除'){
                        resHtml+='<button class="layui-btn layui-btn-sm" style="margin-top: 10px;" onclick="recover('+row.id+')">恢复</button>';
                    }
                    if(row.articlestate=='草稿'){
                        resHtml+='<button class="layui-btn layui-btn-sm layui-btn-normal" style="margin-top: 10px;" onclick="publish('+row.id+')"><i class="layui-icon">&#xe609;</i>发布</button>';
                    }
                    resHtml+='<button class="layui-btn layui-btn-sm layui-btn-warm" style="margin-top: 10px;" onclick="toEditPage('+row.id+')"><i class="layui-icon">&#xe642;</i>编辑</button>';
                    resHtml+='<button class="layui-btn layui-btn-sm" style="margin-top: 10px;" onclick="openPositionWindow('+row.id+')"><i class="layui-icon">&#xe715;</i>位置</button>';
                    resHtml+='<button class="layui-btn layui-btn-sm" style="margin-top: 10px;" onclick="toLook('+row.id+')"><i class="layui-icon">&#xe615;</i>文章</button>';
                    return resHtml;
                }
            },],
    });
});